$(function () {


    function typeTbody(list) {
        var str = '';
        for (var i = 0; i < list.length; i++) {
            str += '<tr data-id="' + list[i].id + '">' +
                '<td>' + list[i].title + '</td>' +
                '<td>' + (list[i].icon == undefined ? '未设置' : list[i].icon) + '</td>' +
                // '<td>' + (list[i].sort == undefined ? '0' : (list[i].sort + 1)) + '</td>' +
                '<td><a class="btn btn-primary btn-xs modify-btn"> 编辑 </a> <a class="btn btn-primary btn-xs delete-btn"> 删除</a></td>' +
                '</tr>'
        }

        return str
    }

    function layerTypes(opts) {
        return '<div style="padding: 10px 0"> ' +
            '<div class="form-group">' +
            '<label class="col-sm-3 control-label">类型名称:<span class="asterisk">*</span></label>' +
            '<div class="col-sm-7">' +
            ' <input  type="text" class="form-control input-sm type-title"  placeholder="文章名称" value="' + opts.title + '" />' +
            '</div>' +
            '</div>' +
            // '<div class="form-group">' +
            // '<label class="col-sm-3 control-label">排序:<span class="asterisk">*</span></label>' +
            // '<div class="col-sm-7">' +
            // ' <input  type="text" class="form-control input-sm type-sort"  placeholder="有小到大" value="' + opts.sort + '" />' +
            // '</div>' +
            // '</div>' +
            '<div class="form-group">' +
            '<label class="col-sm-3 control-label">图标:<span class="asterisk">*</span></label>' +
            '<div class="col-sm-7">' +
            ' <input  type="text" class="form-control input-sm type-icon"  placeholder="图标" value="' + opts.icon + '" />' +
            '</div>' +
            '</div>' +
            '</div>'
    }


    function queryDatum() {
        pageInit({
            pageNum: 1,
            pageSize: 10,
            token: window.localStorage.getItem('token'),
            title: $("#type-title").val() || "",
            ownerName: $("#type-creator").val() || "",
        });
    }

    function resetDatum() {
        pageInit({
            pageNum: 1,
            pageSize: 10,
            token: window.localStorage.getItem('token')
        });
    }


    function pageInit(requestData) {
        var pageInit_layer = layer.load(0);
        $.restPost('/type/doPageQuery', requestData).done(function (msg, data) {

            layer.close(pageInit_layer);

            $("#type-table tbody").html(typeTbody(data.list))


            $("#type-add").off("click").on("click", function () {
                $.rootOpenDialog({
                    title: "新增类型",
                    btn: ["提交"],
                    content: layerTypes({
                        title: "",
                        icon:""
                    }),
                    yes: function (index, lay) {
                        var $lay = $(lay)
                        // console.log($lay.find(".room-title").val())
                        var addRoom_layer1 = layer.load(0);
                        $.restPost('/type/doInsert', {
                            token: window.localStorage.getItem('token'),
                            title: $lay.find(".type-title").val(),
                            icon: $lay.find(".type-icon").val()
                        }).done(function (msg, data) {
                            layer.close(addRoom_layer1)
                            layer.close(index)
                            $.rootSuccessDialog(msg, 2000)
                            pageInit(requestData);
                        }).fail(function (msg) {
                            layer.close(addRoom_layer1)
                            $.rootErrorDialog(msg, 2000)
                        })

                    }
                })


                $("#queryDatum").off("click").on("click", function () {
                    queryDatum()
                });

                $("#resetDatum").off("click").on("click", function () {
                    resetDatum()
                });

                laypage({
                    cont: 'page', //容器。
                    pages: data.pages, //通过后台拿到的总页数
                    curr: requestData.pageNum || 1, //当前页
                    skip: true, //是否开启跳页
                    skin: 'molv', //皮肤,
                    jump: function (obj, first) { //触发分页后的回调
                        requestData.pageNum = obj.curr;
                        if (!first) { //点击跳页触发函数自身，并传递当前页：obj.curr
                            pageInit(requestData);
                        }
                    }
                });
            });

            $('.modify-btn').off('click').on('click',function(){
                var $this = $(this);
                $.restPost('/type/doSelect', {
                    token: window.localStorage.getItem('token'),
                    id:$this.parents('tr').data('id')
                }).done(function (msg, data) {
                    $.rootOpenDialog({
                        title: "新增类型",
                        btn: ["提交"],
                        content: layerTypes({
                            title: data.title,
                            icon: data.icon == undefined?'':data.icon
                        }),
                        yes: function (index, lay) {
                            var $lay = $(lay)
                            // console.log($lay.find(".room-title").val())
                            var addRoom_layer1 = layer.load(0);
                            $.restPost('/type/doUpdate', {
                                token: window.localStorage.getItem('token'),
                                title: $lay.find(".type-title").val(),
                                icon: $lay.find(".type-icon").val(),
                                id:data.id
                            }).done(function (msg, data) {
                                layer.close(addRoom_layer1)
                                layer.close(index)
                                $.rootSuccessDialog(msg, 2000)
                                pageInit(requestData);
                            }).fail(function (msg) {
                                layer.close(addRoom_layer1)
                                $.rootErrorDialog(msg, 2000)
                            })

                        }
                    })
                });
            });

            $('.delete-btn').off('click').on('click',function(){
                var $this = $(this);
                layer.confirm('确定删除这条记录吗？', {
                    btn: ['确认','取消'] //按钮
                }, function(index){
                    $.restPost('/type/doRemove/' + $this.parents('tr').data('id'), {
                        token: window.localStorage.getItem('token')
                    }).done(function (msg, data) {
                        $.rootSuccessDialog(msg, 2000);
                        pageInit(requestData);
                        layer.close(index);
                    });
                });
            });
        })
    }

    pageInit({
        pageNum: 1,
        pageSize: 10,
        token: window.localStorage.getItem('token')
    })
})

